package com.redijedi.tapestry.components;

import java.util.HashMap;
import java.util.Map;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.AfterRenderTemplate;
import org.apache.tapestry.annotations.BeforeRenderTemplate;
import org.apache.tapestry.annotations.BeginRender;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.services.Environment;

/**
 * @author torr
 * 
 */
public class TabGroup {

	@Inject
	private ComponentResources _resources;

	@Inject
	private Environment _environment;

	@Parameter(value = "prop:componentResources.id", defaultPrefix = "literal")
	private String _id;

	@Parameter(value = "prop:componentResources.elementName", defaultPrefix = "literal")
	private String _elementName;

	@Parameter(defaultPrefix = "literal")
	private String _activePanel;

	public String getId() {
		return _id;
	}

	protected String getActivePanel() {
		return _activePanel;
	}

	protected void setActivePanel(String panel) {
		_activePanel = panel;
	}

	@BeginRender
	void setup() {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("tabGrouplId", getId());
		data.put("activePanelId", _activePanel);
		_environment.push(Map.class, data);
	}

	@BeforeRenderTemplate
	void renderBeginTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.element(_elementName);
			writer.attributes("id", getId());
			_resources.renderInformalParameters(writer);
		}
	}

	@AfterRenderTemplate
	void renderEndTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.end();
		}
	}
}
